package com.bdc.whatwhat.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewStub;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;

import com.actionbarsherlock.internal.nineoldandroids.animation.Animator;
import com.actionbarsherlock.internal.nineoldandroids.animation.AnimatorSet;
import com.actionbarsherlock.internal.nineoldandroids.animation.ObjectAnimator;

/**
 * Created by florentchampigny on 14/07/14.
 */
public class Circle extends View {

    int openDuration = 1000;
    int openScale = 300;
    Interpolator interpolator = new AccelerateDecelerateInterpolator();
    boolean isOpen = false;

    View viewToShow;

    public Circle(Context context) {
        super(context);
    }

    public Circle(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Circle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setViewToShow(View v) {
        viewToShow = v;
        viewToShow.setVisibility(GONE);
    }

    public void open() {

        //eviter de faire planter l'anim si on n'a pas mit de vue
        if (viewToShow == null)
            viewToShow = new ViewStub(getContext());

        final AnimatorSet anim = new AnimatorSet();
        anim.setDuration(openDuration);
        anim.setInterpolator(interpolator);
        anim.playTogether(
                ObjectAnimator.ofFloat(this, "scaleX", 1, openScale),
                ObjectAnimator.ofFloat(this, "scaleY", 1, openScale)
        );
        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                isOpen = true;
                viewToShow.setVisibility(VISIBLE);
                ObjectAnimator.ofFloat(viewToShow, "alpha", 0f, 1f).setDuration(300).start();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

        anim.start();

    }

    public void close() {
        AnimatorSet anim = new AnimatorSet();
        anim.setDuration(openDuration);
        anim.setInterpolator(new AccelerateInterpolator());
        anim.playTogether(
                ObjectAnimator.ofFloat(this, "scaleX", openScale, 1),
                ObjectAnimator.ofFloat(this, "scaleY", openScale, 1),
                ObjectAnimator.ofFloat(viewToShow, "alpha", 1f, 0f)
        );
        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                setVisibility(View.INVISIBLE);
                isOpen = false;
                viewToShow.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        anim.start();
    }

    public boolean onBackPressed() {
        if (isOpen) {
            close();
            return true;
        }
        return false;
    }

    public boolean isVisible() {
        return true;
    }
}
